package cate.game.pvp;

import cate.game.GameBody;
import cate.game.db.facade.KvLoader;
import cate.game.db.mongo.kv.KvMongo;
import cate.game.framework.ModuleMan;
import cate.game.friend.aid.FriendAidSPO;
import cate.game.pvp.arena.ArenaManager;
import cate.game.pvp.championrace.GameChampionRaceManager;
import cate.game.pvp.eliterace.EliteRaceManager;
import cate.game.pvp.gradepvp.GradePvpManager;
import cate.game.pvp.guildwar.GuildWarManager;
import cate.game.pvp.jungle.JungleManager;
import cate.game.pvp.ladder.LadderManager;
import cate.game.pvp.minecraft.MinecraftManager;
import cate.game.pvp.pointrace.PointRaceManager;
import cate.game.pvp.throne.TBManager;
import cate.game.pvp.teampvp.TeamPvpManager;
import cp.solution.ops.status.ModOprStatus;
import lombok.extern.slf4j.Slf4j;

import static cate.game.db.mongo.kv.KvMongo.KEY_PVP_RANK_REWARD_EXTRA;

/**
 * pvp玩法模块
 */
@Slf4j
public class PvpManager extends ModuleMan {
    public PointRaceManager pointRace = new PointRaceManager();
    public EliteRaceManager eliteRace = new EliteRaceManager();
    public LadderManager ladder = new LadderManager();
    public MinecraftManager minecraft = new MinecraftManager();
    public ArenaManager arena = new ArenaManager();
    public GuildWarManager guildWar = new GuildWarManager();
    public GameChampionRaceManager championRace = new GameChampionRaceManager();
    public JungleManager jungle = new JungleManager();
    public TBManager throne = new TBManager();
    public GradePvpManager gradePvp = new GradePvpManager();
    public TeamPvpManager teamPvp = new TeamPvpManager();

    public PvpRankRewardExtraKV status;

    @Override
    public void initIndependent(GameBody game) throws Exception {
        super.initIndependent(game);
        pointRace.initIndependent(game);
        eliteRace.initIndependent(game);
        ladder.initIndependent(game);
        minecraft.initIndependent(game);
        arena.initIndependent(game);
        guildWar.initIndependent(game);
        championRace.initIndependent(game);
        jungle.initIndependent(game);
        throne.initIndependent(game);
        gradePvp.initIndependent(game);
        teamPvp.initIndependent(game);
    }

    @Override
    public void initDependent() throws Exception {
        pointRace.initDependent();
        eliteRace.initDependent();
        ladder.initDependent();
        minecraft.initDependent();
        arena.initDependent();
        guildWar.initDependent();
        championRace.initDependent();
        jungle.initDependent();
        throne.initDependent();
        gradePvp.initDependent();
        teamPvp.initDependent();
        // 加载数据
        status = new KvLoader<>(game, PvpRankRewardExtraKV.class, KvMongo.KEY_PVP_RANK_REWARD_EXTRA).getWithMerge((olds) -> {
            PvpRankRewardExtraKV _status = new PvpRankRewardExtraKV();
            _status.initialize();
            for(PvpRankRewardExtraKV old : olds) {
                _status.eat(old);
            }
            return _status;
        });
    }

    @Override
    public synchronized void shutdown() {
        pointRace.shutdownSafe();
        eliteRace.shutdownSafe();
        ladder.shutdownSafe();
        minecraft.shutdownSafe();
        arena.shutdownSafe();
        guildWar.shutdownSafe();
        championRace.shutdownSafe();
        throne.shutdownSafe();
        gradePvp.shutdownSafe();
        teamPvp.shutdownSafe();
        // 存储数据
        game.db.kv.save(status);
    }

    public void tick() {
        try {
            pointRace.tick();
            eliteRace.tick();
            ladder.tick();
            guildWar.tick();
            championRace.tick();
            gradePvp.tick();
            throne.tick();
        } catch (Throwable cause) {
            log.error("Pvp tick发生错误!", cause);
        }
    }

    @Override
    public void onDaySpan() {
        ladder.onDaySpan();
        guildWar.onDaySpan();
    }

    @Override
    public ModOprStatus handleMergeSettle() {
        return minecraft.handleMergeSettle();
    }
}
